using System;
using System.IO;
using DocumentFormat.OpenXml.Packaging;

namespace MailMerge.Infrastructure
{
	public class DocumentBuilder<T> : IDisposable
	{
		public virtual string TemplateFileName { get; set; }

		public DocumentBuilder(T model)
		{
			Model = model;
		}

		public T Model { get; set; }

		public WordprocessingDocument Document { get; set; }

		public virtual void Build(string outputPath)
		{
			if (outputPath == null) throw new ArgumentNullException("outputPath");
			var currentDir = Directory.GetCurrentDirectory();
			var path = Path.Combine(currentDir, TemplateFileName);
			File.Copy(path, outputPath, true);
			Document = WordprocessingDocument.Open(outputPath, true);
		}

		public void Dispose()
		{
			Document.Dispose();
		}
	}
}